CKEDITOR.dialog.add( 'engonlinelinkDialog', function ( editor ) {
    return {
        title: 'Create a link to an EngOnline page',
        minWidth: 400,
        minHeight: 200,
        contents: [
            {
                id: 'tab-basic',
                label: 'Basic Settings',
                elements: [
                    {
		                type: 'text',
		                id: 'link-text',
		                label: 'Link text',
		                validate: CKEDITOR.dialog.validate.notEmpty( "Link text field cannot be empty." )
		            },
					{
						type: 'select',
						id: 'page',
						label: 'Select the EngOnline page',
						validate: CKEDITOR.dialog.validate.notEmpty( "You must choose a page." ),
						items: [ [ '--- Select a page ---', null ] ],
						onLoad: function(element) {
							var select = this;

							$.getJSON('/book/pages/all/', function(data) {
								console.log("Received pages.");
								var dataArray = [];
								$.each(data, function (index, value) {
									var set = value['fields'];
									select.add(set.title, set.slug_title);
							    });
							});
						}
					}
                ]
            }
        ],
        onOk: function() {
            var dialog = this;

            var engonlinelink = editor.document.createElement( 'a' );
            engonlinelink.setAttribute('href', '#');
            engonlinelink.setAttribute('engonline-page', dialog.getValueOf('tab-basic', 'page'));
            engonlinelink.setText(dialog.getValueOf('tab-basic', 'link-text'));
            editor.insertElement( engonlinelink );
        }
    };
});